package com.faith.timefloating;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;

import com.faith.timefloating.adapter.MySpinnerAdapter;
import com.faith.timefloating.myutils.Constants;
import com.faith.timefloating.R;
import com.google.analytics.tracking.android.EasyTracker;

public class Laucher extends Activity {

	private EditText fontsize;
	private SharedPreferences shared;
	private int notificationId = 212;

	// private NotificationHelper notiHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		Intent startIntent = new Intent(this, TimeFloatingService.class);
		startIntent.putExtra("centerX", width / 2);
		startIntent.putExtra("centerY", height / 2);
		startService(startIntent);
		setContentView(R.layout.main);
		EasyTracker.getInstance().setContext(this);
		shared = getSharedPreferences(Constants.SHARED_NAME, MODE_PRIVATE);
		final int font_color_index = shared.getInt(Constants.FONT_COLOR_KEY, 0);
		final int font_size = shared.getInt(Constants.FONT_SIZE_KEY, 15);
		final String displayMode = shared.getString(Constants.TIME_MODE,
				Constants.MODE.LONG_MODE.toString());
		final CheckBox shortMode = (CheckBox) findViewById(R.id.shortmode);
		final CheckBox longMode = (CheckBox) findViewById(R.id.longmode);
		shortMode
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							longMode.setChecked(false);
						} else {
							longMode.setChecked(true);
						}
					}
				});
		longMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					shortMode.setChecked(false);
				} else {
					shortMode.setChecked(true);
				}
			}
		});
		if (displayMode.equalsIgnoreCase(Constants.MODE.LONG_MODE.toString())) {
			longMode.setChecked(true);
			shortMode.setChecked(false);
		} else {
			longMode.setChecked(false);
			shortMode.setChecked(true);
		}
		final Spinner spinner = (Spinner) findViewById(R.id.fontcolor);
		String[] arr = getResources().getStringArray(R.array.color_arrs);
		MySpinnerAdapter spinnerAdapter = new MySpinnerAdapter(this, arr);
		spinner.setAdapter(spinnerAdapter);
		spinner.setSelection(font_color_index, true);
		fontsize = (EditText) findViewById(R.id.fontsize);
		fontsize.setText(font_size + "");
		fontsize.setOnFocusChangeListener(new View.OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {
					if (!fontsize.getText().toString().equals("")
							&& Integer.valueOf(fontsize.getText().toString()) > 15) {
						fontsize.setText("15");
					}
					if (!fontsize.getText().toString().equals("")
							&& Integer.valueOf(fontsize.getText().toString()) < 9) {
						fontsize.setText("9");
					}
					fontsize.clearFocus();
				} else {
					fontsize.selectAll();
				}
			}
		});
		fontsize.setOnKeyListener(new View.OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// if(event.getAction()==KeyEvent.ACTION_UP){
				if (!fontsize.getText().toString().equals("")
						&& Integer.valueOf(fontsize.getText().toString()) > 30) {
					fontsize.setText("30");
				}
				// if(!fontsize.getText().toString().equals("")&&Integer.valueOf(fontsize.getText().toString())<9){
				// fontsize.setText("9");
				// fontsize.selectAll();
				// }
				// }
				return false;
			}
		});
		Button close = (Button) findViewById(R.id.close);
		close.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				EasyTracker.getTracker().sendEvent("Button Click", "Click Refresh Event", "Click Refresh "+(longMode.isChecked()?" with long time mode":"with short time mode"), Long.valueOf(1));
				String _fsize = fontsize.getText().toString();
				if (_fsize.equals("") || Integer.valueOf(_fsize) < 9) {
					fontsize.setText("9");
					fontsize.selectAll();
					fontsize.requestFocus();
					return;
				}
				int new_color_index = spinner.getSelectedItemPosition();
				int new_font_size = Integer.valueOf(fontsize.getText()
						.toString());
				String new_mode = shortMode.isChecked() ? Constants.MODE.SHORT_MODE
						.toString() : Constants.MODE.LONG_MODE.toString();
				shared.edit().putInt(Constants.FONT_COLOR_KEY, new_color_index)
						.commit();
				shared.edit().putInt(Constants.FONT_SIZE_KEY, new_font_size)
						.commit();
				shared.edit().putString(Constants.TIME_MODE, new_mode).commit();
				if (new_color_index != font_color_index
						|| new_font_size != font_size
						|| displayMode != new_mode) {
					// refresh view;
					Intent intent = new Intent(Laucher.this,
							TimeFloatingService.class);
					Bundle bundle = new Bundle();
					bundle.putInt(Constants.FONT_COLOR_KEY, new_color_index);
					bundle.putInt(Constants.FONT_SIZE_KEY, new_font_size);
					bundle.putString(Constants.TIME_MODE, new_mode);
					bundle.putBoolean(Constants.ACTION_REFRESH, true);
					intent.putExtras(bundle);
					startService(intent);
					// notiHelper.updateNotification(notificationId,
					// getResources().getString(R.string.refresh));
				} else if (TimeFloatingService.isHide()) {
					Intent intent = new Intent(Laucher.this,
							TimeFloatingService.class);
					startService(intent);
				}
			}
		});
		// Notification
		// notiHelper=new NotificationHelper(this);
		// Intent notifyIntent = new Intent();
		// notifyIntent.setClass(this, Laucher.class);
		// PendingIntent pending = PendingIntent.getActivity(this, 0,
		// notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		// notiHelper.createNotification(notificationId,
		// getResources().getString(R.string.app_name),
		// getResources().getString(R.string.mytime),"TICKER? ", pending);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getTracker().sendView("Launcher Screen");
	}


}
